<template>
    <div class="Banner_content">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>首页banner</el-breadcrumb-item>
      </el-breadcrumb>
      <h1>上传banner：</h1>
      <el-upload
        class="upload-demo"
        action="/api/Banner/uploads" :on-remove="handleRemove" :on-success="handleSuccess"
        :file-list="Banner" name="image"
        list-type="picture">
        <i class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      <el-button type="success" round @click="addBanner">提交</el-button>
  </div>

</template>

<script>

import {mapState} from "vuex";

export default {
  name: "banner",
  data() {
    return {
      imageUrl:'',
      FileList:[]
    }
  },
  created() {
    this.FileList=this.Banner
  },
  computed:{...mapState({
      Banner:state=>state.home.BannerList
    })},
  beforeCreate() {
    this.$store.dispatch('home/getBanner')
  },
  methods:{
    handleRemove(file, fileList) {
      this.FileList=fileList
    },
    handleSuccess(response){
      this.imageUrl=response
    },
   async addBanner(){
      const res = await this.$http.post('/api/banner/addBanner',{imageUrl:this.imageUrl})
      if (res.data.status === 200) {
        return this.$message.success('上传成功')
      }
    else {
      return this.$message.error('上传失败 请检查图片格式')
    }
    }

  }

}
</script>

<style scoped lang="less">
.Banner_content {
  width: 600px;
  padding: 20px;
  text-align: center;
}
  h1{
    text-align: left;
    margin-top: 30px;
    margin-bottom: 20px;
  }

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
/deep/ .el-upload-list{
  display: flex;
  justify-content: space-between;
}
.avatar-uploader-icon {
font-size: 28px;
color: #8c939d;
width: 178px;
height: 178px;
line-height: 178px;
text-align: center;
border: 1px solid;
}
.avatar {
width: 178px;
height: 178px;
display: block;
}
.el-button{
margin-top: 10px;
}
</style>
